/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.windesheim.archdemo.presentationmodel;

import nl.windesheim.archdemo.dao.ProductDao;
import nl.windesheim.archdemo.model.Product;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import nl.windesheim.archdemo.dao.GlobalInstances;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

/**
 *
 * @author re0053273
 */
public class ProductTablePresenter implements TableModel {

    private ProductDao productDao = GlobalInstances.getProductDao();
    /* use only through getter because of lazy loading! */
    private List<Product> products;
    private List<TableModelListener> listeners;
       
    /**
     * Lazy loading of Product list
     * @return 
     */
    private List<Product> getProducts(){
        if( this.products == null ){
            this.products = productDao.getAll();
        }
        return this.products;
    }
   

    public int getColumnCount() {
        return 5;
    }

    public String getColumnName(int i) {
        switch(i){
            case 0: return "id";
            case 1: return "Naam";
            case 2: return "Code";
            case 3: return "Prijs";
            case 4: return "Voorraad";
            default: throw new ArrayIndexOutOfBoundsException("Product table has only 5 cols");
        } 
    }

    public int getRowCount() {
        return this.getProducts().size();
    }

    public Product getRowModel(int row){
        return this.getProducts().get(row);
    }
    
    public Object getValueAt(int row, int col) {
        Product o = this.getProducts().get(row);
        Object value = null;
        switch(col){
            case 0: 
                value = o.getId();
                break;
            case 1: 
                value = o.getName();
                break;
            case 2: value = o.getSerial();
                break;
            case 3: 
                value = o.getPrice();
                break;
            case 4: 
                value = o.getStock();
                break;
            default: throw new ArrayIndexOutOfBoundsException("Product table has only 5 cols");
        } 
        if( value == null ){
            return "";
        }
        return value.toString();
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false;
    }
    
    @Override
    public void setValueAt(Object o, int i, int i1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void addTableModelListener(TableModelListener tl) {
        if( listeners == null ){
            listeners = new ArrayList<TableModelListener>(1);
            listeners.add(tl);
        }
    }    
    public void removeTableModelListener(TableModelListener tl) {
        if(listeners != null){
            listeners.remove(tl);
        }
    }

}
